import { createRouter, createWebHistory } from 'vue-router'
import LoginView from '../views/LoginView.vue'
import RegisterView from "../views/RegisterView.vue";
import BusinessListView from "../views/BusinessListView.vue";
import IndexView from "../views/IndexView.vue";
import OrderListView from "../views/OrderListView.vue";
import OrderView from "../views/OrderView.vue";
import PaymentView from "../views/PaymentView.vue";
import BusinessNewView from "../views/BusinessNewView.vue";
import AddressView from "../views/AddressView.vue";
import NewAddressView from "../views/NewAddressView.vue";
import ChangeAddressView from "../views/ChangeAddressView.vue";
import MapView from "../views/MapView.vue";
import axios from "axios";
import errorView from "../views/errorView.vue";

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'Home',
      component: IndexView
    },
    {
      path:'/BusinessList',
      name:'BusinessList',
      component:BusinessListView
    },
    {
      path:'/BusinessNew',
      name:'BusinessNew',
      component:BusinessNewView
    },
    {
      path:'/Index',
      name:'Index',
      component:IndexView
    },
    {
      path:'/Login',
      name:'Login',
      component:LoginView
    },
    {
      path:'/OrderList',
      name:'OrderList',
      component:OrderListView
    },
    {
      path:'/Order',
      name:'Order',
      component:OrderView
    },
    {
      path:'/Payment',
      name:'Payment',
      component:PaymentView
    },
    {
      path:'/Register',
      name:'Register',
      component:RegisterView
    },
    {
      path:'/Address',
      name:'Address',
      component:AddressView
    },
    {
      path:'/ChangeAddress',
      name:'ChangeAddress',
      component:ChangeAddressView
    },
    {
      path:'/NewAddress',
      name:'NewAddress',
      component:NewAddressView
    },
    {
      path:'/Map',
      name:'Map',
      component:MapView
    },
    {
      path:'/Error',
      name:'Error',
      component:errorView
    }
  ]
})

router.beforeEach((to,from,next)=>{
  if(window.location.href==="http://localhost:5173/index"||window.location.href==="http://localhost:5173"){
    next()
  }
  else {
  if(to.path.startsWith('/login')){
/*    window.localStorage.removeItem('access-admin')*/
    next()
  }
  else {
    let admin =JSON.parse(window.localStorage.getItem('access-admin'))
    if(!admin){
      alert("请登录")
      next({path:'/login'})
    }else {
      axios({
        url:'/api/elm_api/UserController/checkToken',
        method:'post',
        headers:{
          token:admin.token
        }
      }).then((res)=>{
        if(!res.data){
          alert(res.data)
          console.log('检验失败')
          router.push('/login')
        }
        else {
          next()
        }
      }).catch((error)=>{
        console.error("异步请求错误：",error)
        next({path:'/error'})
      })
      next()
    }
  }}
})

export default router
